package org.keyyond.ms.business.service.impl;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.keyyond.ms.business.common.CommonService;
import org.keyyond.ms.business.dao.MsBusinessMessageLogDao;
import org.keyyond.ms.business.dao.MsBusinessParamDao;
import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.model.MsBusinessParam;
import org.keyyond.ms.business.model.vo.MsBusinessParamVo;
import org.keyyond.ms.business.service.MsBusinessParamService;
import org.keyyond.ms.business.util.Constant;
import org.keyyond.ms.core.exception.FDFBexception;
import org.keyyond.ms.core.util.JsonUtil;
import org.keyyond.ms.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author zhuyong
 * @belong to Shccs
 * @date:2018-1-22(V1.0)
 * @date:2018-1-22(V1.0)
 * @version : V1.0
 */
@Service
public class MsBusinessParamServiceImpl extends CommonService implements MsBusinessParamService{
	private static Logger logger = Logger.getLogger(MsBusinessParamServiceImpl.class);
	@Autowired
	private MsBusinessParamDao paramDao;
	
	@Autowired
	private MsBusinessMessageLogDao messageLogDao;
	
	public String selectParamByCode(HttpServletRequest request,String param) {
		try {
			//保存回执的Json
			String resultJson = null;
			//判断是否为空
			if(StringUtils.isEmpty(param)) resultJson = "{}";
			//判断微服务跟踪ID是否为空
			if(request == null) throw new FDFBexception("请求对象request为Null");
			String traceId = request.getHeader("X-B3-TraceId");
			if(StringUtils.isEmpty(traceId)) throw new FDFBexception("微服务跟踪ID为Null");
			//判断回执的Json是否为Null,不为Null则不查询
			if(resultJson == null){
				//将Json字符串转化为Map对象
				Map<String,String> map = JsonUtil.JsonToMap(param);
				//判断参数是否为空,为空则不查询并返回{},其他则调用Dao层方法查询配置信息
				if(map == null){
					throw new FDFBexception("将传入的param转成Map失败");
				}
				else if(StringUtils.isEmpty(map.get("code"))){
					resultJson = "{}";
				}
				else{
					//调用Dao层查询的方法
					MsBusinessParam msbusinessparam = paramDao.selectByCode(map.get("code"));
					if(msbusinessparam == null) {
						resultJson = "{}";
					}else{
						//vo对象
						MsBusinessParamVo msbusinessparamvo = new MsBusinessParamVo();
						//复制属性值
						msbusinessparamvo = (MsBusinessParamVo) copyBean(msbusinessparam, msbusinessparamvo);
						//将vo对象转化为Json字符串
						resultJson = JsonUtil.toJson(msbusinessparamvo);
						if(resultJson == null) throw new FDFBexception("将查询出的配置对象转换成Json失败");
					}
				}
			}
			//记录日志
			MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
			messageLog.setLogtype(Constant.CONST_LOGTYPE_SERVICE_CALL);//日志类型
			messageLog.setContent(StringUtils.isEmpty(param) ? "" : StringUtil.cutString(param, 1024, "GBK"));//截取长度,推送参数
			messageLog.setReturnmessage(StringUtil.cutString(resultJson, 1024, "GBK"));//截取长度,返回消息
			messageLog.setTraceid(traceId);//微服务跟踪ID
			messageLog.setCreateby(Constant.CONST_SYSTEM);//创建人
			messageLog.setModifyby(Constant.CONST_SYSTEM);//修改人
			try {
				messageLogDao.insert(messageLog);//调用Dao层添加日志的方法
			} catch (Exception e) {
				e.printStackTrace();
				throw new FDFBexception("通过Code查询系统配置表成功后添加日志失败>>>" + e.getMessage());
			}
			return resultJson;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("参数:" + param + ">>>通过Code查询系统配置表失败>>>" + e.getMessage());
			//记录日志
			MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
			messageLog.setLogtype(Constant.CONST_LOGTYPE_EXCEPTION);
			messageLog.setContent(StringUtils.isEmpty(param) ? "" : StringUtil.cutString(param, 1024, "GBK"));//截取长度,推送参数
			messageLog.setReturnmessage(StringUtil.cutString(e.getMessage(), 1024, "GBK"));//截取长度,返回消息
			messageLog.setCreateby(Constant.CONST_SYSTEM);//创建人
			messageLog.setModifyby(Constant.CONST_SYSTEM);//修改人
			try {
				messageLogDao.insert(messageLog);//调用Dao层添加日志的方法
			} catch (Exception e2) {
				e2.printStackTrace();
				logger.error("通过Code查询系统配置表发生异常后添加日志失败>>>" + e2.getMessage());
			}
			return "{\"false\":\""+ e.getMessage() +"\"}";
		}
	}
}
